/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.DataProvider;
import DTO.ChuDeDTO;
import DTO.WebsiteDTO;
import Threads.getNews;
import Threads.loadTinTuc;
import Threads.loadTreeWebsite;
import java.io.File;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author QuangHau
 */
public class Main extends javax.swing.JFrame {

    /**
     * Creates new form Main
     */
    public static boolean reset = false;

    public Main() {
        initComponents();
        GUI.ShowCenterScreen(this);

        if (!DataProvider.Successful()) {
            ConnectionDatabase jdCon = new ConnectionDatabase(this, true, true);
            GUI.ShowCenterScreen(jdCon);
            jdCon.setVisible(true);
        }

        File f = new File("Image");
        if(!f.exists()){
            f.mkdir();
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnContent = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        treeWebsite = new javax.swing.JTree();
        pnNews = new javax.swing.JScrollPane();
        lbStatus = new javax.swing.JLabel();
        btnThoat = new javax.swing.JButton();
        btnQuanLy = new javax.swing.JButton();
        btnLayTin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chương trình lấy tin tự động");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        pnContent.setBackground(new java.awt.Color(255, 255, 255));
        pnContent.setDividerLocation(170);
        pnContent.setDividerSize(2);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        treeWebsite.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        treeWebsite.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        treeWebsite.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeWebsiteValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(treeWebsite);

        pnContent.setLeftComponent(jScrollPane1);

        pnNews.setBackground(new java.awt.Color(255, 255, 255));
        pnContent.setRightComponent(pnNews);

        lbStatus.setText("aaa");

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnQuanLy.setText("Quản Lý");
        btnQuanLy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuanLyActionPerformed(evt);
            }
        });

        btnLayTin.setText("Lấy tin");
        btnLayTin.setEnabled(false);
        btnLayTin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLayTinActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnContent)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 431, Short.MAX_VALUE)
                .addComponent(btnLayTin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnQuanLy)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnThoat)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnLayTin, btnQuanLy, btnThoat});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(pnContent, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbStatus)
                    .addComponent(btnThoat)
                    .addComponent(btnQuanLy)
                    .addComponent(btnLayTin))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLayTinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLayTinActionPerformed
        getNews gw = new getNews(new Object[]{treeWebsite, pnNews, lbStatus, btnLayTin});
        gw.start();
    }//GEN-LAST:event_btnLayTinActionPerformed

    private void btnQuanLyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuanLyActionPerformed
        Manager manager = new Manager(this, true);
        GUI.ShowCenterScreen(manager);
        manager.setVisible(true);
        if (manager.isReset) {
            loadTreeWebsite t = new loadTreeWebsite(new Object[]{treeWebsite, pnNews, lbStatus, btnLayTin, -1, -1});
            t.start();
        }
    }//GEN-LAST:event_btnQuanLyActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnThoatActionPerformed

    private void treeWebsiteValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeWebsiteValueChanged
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treeWebsite.getLastSelectedPathComponent();
        if (treeNode == null) {
            loadTinTuc ltt = new loadTinTuc(new Object[]{pnNews, lbStatus, -1, -1, true});
            ltt.start();
            return;
        }

        if (treeNode.isRoot()) {
            loadTinTuc ltt = new loadTinTuc(new Object[]{pnNews, lbStatus, -1, -1, true});
            ltt.start();
        } else if (treeNode.getUserObject() instanceof ChuDeDTO) {
            ChuDeDTO chudeDTO = (ChuDeDTO) treeNode.getUserObject();
            loadTinTuc ltt = new loadTinTuc(new Object[]{pnNews, lbStatus, -1, chudeDTO.getMaChuDe(), true});
            ltt.start();
        } else {
            WebsiteDTO websiteDTO = (WebsiteDTO) treeNode.getUserObject();
            loadTinTuc ltt = new loadTinTuc(new Object[]{pnNews, lbStatus, websiteDTO.getMaWebsite(), -1, true});
            ltt.start();
        }
    }//GEN-LAST:event_treeWebsiteValueChanged

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        loadTreeWebsite t = new loadTreeWebsite(new Object[]{treeWebsite, pnNews, lbStatus, btnLayTin, -1, -1});
        t.start();
    }//GEN-LAST:event_formWindowOpened

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        if (reset) {
            treeWebsiteValueChanged(null);
            reset = false;
        }
    }//GEN-LAST:event_formWindowActivated

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLayTin;
    private javax.swing.JButton btnQuanLy;
    private javax.swing.JButton btnThoat;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbStatus;
    private javax.swing.JSplitPane pnContent;
    private javax.swing.JScrollPane pnNews;
    private javax.swing.JTree treeWebsite;
    // End of variables declaration//GEN-END:variables
}
